package com.sinochem.yunlian.ship.common.annotation;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;

/**
 * @Auther: zzd
 * @Dscription: 手机号码校验器
 * @Date: Create in 2017/11/27 17:49
 **/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PhoneValidate.PhoneValidationUtils.class)
@Documented
public @interface PhoneValidate {

    String message() default "手机号码不正确";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class PhoneValidationUtils implements ConstraintValidator<PhoneValidate,String>
    {

        @Override
        public void initialize(PhoneValidate arg0)
        {
        }

        @Override
        public boolean isValid(String phone, ConstraintValidatorContext context)
        {
            if(phone==null){
                return true;
            }
            String regex="^[1][3,4,5,7,8][0-9]{9}$";
            if(phone.length()<=0){
                return false;
            }
            return phone.matches(regex);
        }

    }





}
